package com.key.common.service.serviceImp;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.key.common.model.Page;
import com.key.common.service.PagingFactory;
import com.key.util.HibernateUtil;

@Service
@Transactional
public class PagingFactoryImpl implements PagingFactory {

	public Integer queryObjCount(Page page) {
		StringBuffer queryHQL = new StringBuffer("SELECT COUNT(*) FROM "
				+ page.getObjName());
		// 如果查询条件不为空
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		Session session = HibernateUtil.getSessionFactory().openSession();
		Integer i = Integer.parseInt(session.createQuery(queryHQL.toString())
				.list().get(0).toString());
		session.close();
		return i;
	}

	public Page queryObjList(Page page) {
		StringBuffer queryHQL = new StringBuffer();
		if (page.getQueryField() != null
				&& !page.getQueryField().trim().equals(""))
			queryHQL.append("SELECT " + page.getQueryField() + " FROM "
					+ page.getObjName());
		else
			queryHQL.append("FROM " + page.getObjName());
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		if (page.getOrderBy() != null)
			queryHQL.append(" order by " + page.getOrderBy());
		System.out.println(queryHQL.toString());
		Integer i = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery(queryHQL.toString());
		query.setFirstResult(page.getStartIndex());
		query.setMaxResults(page.getPageSize());
		page.setTotalCount(this.queryObjCount(page));
		page.setObjList(query.list());
		session.close();
		return page;
	}

	public Page querySapPart(Page page) {
		StringBuffer queryHQL = new StringBuffer();
		if (page.getQueryField() != null
				&& !page.getQueryField().trim().equals(""))
			queryHQL.append("SELECT " + page.getQueryField() + " FROM "
					+ page.getObjName());
		else
			queryHQL.append("FROM " + page.getObjName());
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		if (page.getOrderBy() != null)
			queryHQL.append(" order by " + page.getOrderBy());
		System.out.println(queryHQL.toString());
		Integer i = null;
		Session session = HibernateUtil.getSapPartSessionFactory()
				.openSession();
		Query query = session.createQuery(queryHQL.toString());
		query.setFirstResult(page.getStartIndex());
		query.setMaxResults(page.getPageSize());
		page.setTotalCount(this.querySapPartCount(page));
		page.setObjList(query.list());
		session.close();
		return page;
	}

	public Integer querySapPartCount(Page page) {
		StringBuffer queryHQL = new StringBuffer("SELECT COUNT(*) FROM "
				+ page.getObjName());
		// 如果查询条件不为空
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		Session session = HibernateUtil.getSapPartSessionFactory()
				.openSession();
		Integer i = Integer.parseInt(session.createQuery(queryHQL.toString())
				.list().get(0).toString());
		session.close();
		return i;
	}

	public Page queryHrPart(Page page) {
		StringBuffer queryHQL = new StringBuffer();
		if (page.getQueryField() != null
				&& !page.getQueryField().trim().equals(""))
			queryHQL.append("SELECT " + page.getQueryField() + " FROM "
					+ page.getObjName());
		else
			queryHQL.append("FROM " + page.getObjName());
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		if (page.getOrderBy() != null)
			queryHQL.append(" order by " + page.getOrderBy());
		System.out.println(queryHQL.toString());
		Integer i = null;
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		Query query = session.createQuery(queryHQL.toString());
		query.setFirstResult(page.getStartIndex());
		query.setMaxResults(page.getPageSize());
		page.setTotalCount(this.queryHrCount(page));
		page.setObjList(query.list());
		session.close();
		return page;
	}

	public Integer queryHrCount(Page page) {
		StringBuffer queryHQL = new StringBuffer("SELECT COUNT(*) FROM "
				+ page.getObjName());
		// 如果查询条件不为空
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		Integer i = Integer.parseInt(session.createQuery(queryHQL.toString())
				.list().get(0).toString());
		session.close();
		return i;
	}

	public Page queryHrDelegatePart(Page page) {
		StringBuffer queryHQL = new StringBuffer();
		if (page.getQueryField() != null
				&& !page.getQueryField().trim().equals(""))
			queryHQL.append("SELECT " + page.getQueryField() + " FROM "
					+ page.getObjName());
		else
			queryHQL.append("FROM " + page.getObjName());
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		if (page.getOrderBy() != null)
			queryHQL.append(" order by " + page.getOrderBy());
		System.out.println(queryHQL.toString());
		Integer i = null;
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		Query query = session.createQuery(queryHQL.toString());
		query.setFirstResult(page.getStartIndex());
		query.setMaxResults(page.getPageSize());
		page.setTotalCount(this.queryHrDelegateCount(page));
		page.setObjList(query.list());
		session.close();
		return page;
	}

	public Integer queryHrDelegateCount(Page page) {
		StringBuffer queryHQL = new StringBuffer("SELECT COUNT(*) FROM "
				+ page.getObjName());
		// 如果查询条件不为空
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		Session session = HibernateUtil.getHrIntSessionFactory().openSession();
		Integer i = Integer.parseInt(session.createQuery(queryHQL.toString())
				.list().get(0).toString());
		session.close();
		return i;
	}

	public Page querySsoPart(Page page) {
		StringBuffer queryHQL = new StringBuffer();
		if (page.getQueryField() != null
				&& !page.getQueryField().trim().equals(""))
			queryHQL.append("SELECT " + page.getQueryField() + " FROM "
					+ page.getObjName());
		else
			queryHQL.append("FROM " + page.getObjName());
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		if (page.getOrderBy() != null)
			queryHQL.append(" order by " + page.getOrderBy());
		System.out.println(queryHQL.toString());
		Integer i = null;
		Session session = HibernateUtil.getSsoSessionFactory().openSession();
		Query query = session.createQuery(queryHQL.toString());
		query.setFirstResult(page.getStartIndex());
		query.setMaxResults(page.getPageSize());
		page.setTotalCount(this.querySsoCount(page));
		page.setObjList(query.list());
		session.close();
		return page;
	}

	public Integer querySsoCount(Page page) {
		StringBuffer queryHQL = new StringBuffer("SELECT COUNT(*) FROM "
				+ page.getObjName());
		// 如果查询条件不为空
		if (page.getCondition() != null
				&& !page.getCondition().trim().equals(""))
			queryHQL.append(" WHERE " + page.getCondition());
		Session session = HibernateUtil.getSsoSessionFactory().openSession();
		Integer i = Integer.parseInt(session.createQuery(queryHQL.toString())
				.list().get(0).toString());
		session.close();
		return i;
	}
}